package br.srv.full.virtual.entities.menu;

import java.io.Serializable;
import java.util.List;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import br.srv.full.virtual.bean.base.QueryID;

@Entity
@DiscriminatorValue(value = "G")
@NamedQueries( {
		@NamedQuery(name = "MenuGroup.findByRoles", query = "select mg  from MenuGroup mg inner join mg.roles as role where role in (?) order by mg.priority"),
		@NamedQuery(name = "MenuGroup.findRolesNull_or_NoRoles", query = "select mg from MenuGroup mg where mg.roles is empty order by mg.priority"),
		@NamedQuery(name = "MenuGroup.findByName", query = "select mg from MenuGroup mg where mg.name = ?1 order by mg.priority") })
public class MenuGroup extends MenuBase implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4186819811308452345L;

	/**
	 * 
	 */
	public static final String NO_GROUP = "noGroup";

	public static final QueryID FIND_BY_ROLES = new QueryID(MenuGroup.class, "MenuGroup.findByRoles",
			new String[] { "rules" });

	public static final QueryID FIND_BY_NAME = new QueryID(MenuGroup.class, "MenuGroup.findByName");

	public static final QueryID FIND_RULES_NULL_OR_NO_RULES = new QueryID(MenuGroup.class,
			"MenuGroup.findRolesNull_or_NoRoles");

	public static final String ROOT_GROUP_NAME = "rootGroup";

	@OneToMany(targetEntity = MenuBase.class, fetch = FetchType.EAGER, mappedBy = "group")
	private List<MenuBase> itens;

	private Boolean expanded = true;

	public MenuGroup() {
		super();
	}

	/**
	 * @param p_string
	 */
	public MenuGroup(String p_name) {
		setName(p_name);
		setLabel(p_name);
	}

	public List<MenuBase> getItens() {
		return itens;
	}

	public void setItens(List<MenuBase> p_itens) {
		itens = p_itens;
	}

	/**
	 * @return
	 */
	public Boolean isExpanded() {
		if (expanded == null)
			expanded = false;
		return expanded;
	}

	/**
	 * @param p_expanded
	 *            the expanded to set
	 */
	public void setExpanded(Boolean p_expanded) {
		if (p_expanded == null)
			p_expanded = false;
		expanded = p_expanded;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.srv.full.virtual.entities.base.MenuBase#toString()
	 */
	@Override
	public String toString() {
		List<MenuBase> l_itens = getItens();
		String l_itensList;
		if (l_itens == null || l_itens.size() == 0)
			l_itensList = "";
		else
			l_itensList = " SubItens: " + l_itens.toString();
		String l_simpleName = this.getClass().getSimpleName();
		return l_simpleName + ": " + getName() + l_itensList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object p_obj) {
		if (p_obj instanceof MenuGroup) {
			MenuGroup l_grp = (MenuGroup) p_obj;
			return getName().equals(l_grp.getName());
		}
		return false;
	}

	/**
	 * @return
	 */
	@Transient
	public boolean isRootGroup() {
		return getName().equals(NO_GROUP) || getName().equals(ROOT_GROUP_NAME);
	}
}
